package com.lanou3g.bookstore.category.service;

import com.lanou3g.bookstore.category.dao.CategoryDao;
import com.lanou3g.bookstore.category.domain.Category;
import com.lanou3g.bookstore.util.util.JDBCUtil;

import java.util.List;

public class CategoryService {
    private CategoryDao categoryDao = new CategoryDao();

    public List<Category> findAll() {
        try {
            JDBCUtil.begin();
            List<Category> bookClass = categoryDao.findAll();
            return bookClass;
        } finally {
            JDBCUtil.closeConn();
        }
    }

    public String add(Category category) {
        if (category.getCname().equals("")) {
            return "不能添加空的类别";
        }
        boolean hasName = categoryDao.hasName(category.getCname());
        if (hasName) {
            return "该类别已存在";
        }
        categoryDao.add(category);
        return "添加成功";
    }

    public String del(String cid) {
        boolean byCategory = categoryDao.findByCategory(cid);
        if (byCategory) {
            return "该类还存在书籍,无法删除";
        }
        categoryDao.del(cid);
        return "删除成功";
    }

    public Category findByCid(String cid) {
        Category category = categoryDao.findByCid(cid);
        return category;
    }

    public void edit(Category category) {
        categoryDao.edit(category);
    }
}
